package com.work.yshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import com.work.yshop.R;
import com.work.yshop.databinding.ActivityLoginBinding;
import com.work.yshop.mvp.contract.LoginContract;
import com.work.yshop.mvp.presenter.LoginPresenter;

/**
 * 登录页面Activity - MVP架构
 */
public class LoginActivity extends AppCompatActivity implements LoginContract.View {

    private ActivityLoginBinding binding;
    private LoginPresenter presenter;
    private RadioGroup rgUserType;
    private RadioButton rbBuyer, rbSeller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 设置状态栏
        setupStatusBar();
        
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 初始化Presenter
        presenter = new LoginPresenter();
        presenter.attachView(this);

        // 初始化视图
        initViews();
        
        // 设置监听器
        setupListeners();
        
        // 更新时间
        presenter.updateTime();
        
        // 预填充默认账号密码
        binding.etPhone.setText("18888888888");
        binding.etPassword.setText("123456");
    }
    
    /**
     * 初始化视图
     */
    private void initViews() {
        rgUserType = findViewById(R.id.rgUserType);
        rbBuyer = findViewById(R.id.rbBuyer);
        rbSeller = findViewById(R.id.rbSeller);
        
        // 默认选择买家
        rbBuyer.setChecked(true);
    }
    
    /**
     * 设置状态栏
     */
    private void setupStatusBar() {
        // 让内容区域延伸到状态栏
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
        
        // 设置状态栏为浅色，图标为深色
        WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
        controller.setAppearanceLightStatusBars(true);
        
        // 设置状态栏透明
        getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
    }

    /**
     * 设置各种监听器
     */
    private void setupListeners() {
        // 登录按钮点击事件
        binding.btnLogin.setOnClickListener(v -> {
            String phone = binding.etPhone.getText().toString().trim();
            String password = binding.etPassword.getText().toString().trim();
            
            // 获取用户类型
            int userType = rbBuyer.isChecked() ? 
                    LoginContract.USER_TYPE_BUYER : LoginContract.USER_TYPE_SELLER;
            
            presenter.login(phone, password, userType);
        });

        // 注册账号点击事件
        binding.tvRegister.setOnClickListener(v -> {
            // 跳转到注册页面
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        // 忘记密码点击事件
        binding.tvForgetPassword.setOnClickListener(v -> {
            // 跳转到忘记密码页面（此处未实现忘记密码页面，仅Toast提示）
            Toast.makeText(LoginActivity.this, "找回密码功能暂未开放", Toast.LENGTH_SHORT).show();
        });

        // 微信登录点击事件
        binding.llWechatLogin.setOnClickListener(v -> {
            Toast.makeText(LoginActivity.this, "微信登录功能暂未开放", Toast.LENGTH_SHORT).show();
        });

        // 短信登录点击事件
        binding.llSmsLogin.setOnClickListener(v -> {
            Toast.makeText(LoginActivity.this, "短信登录功能暂未开放", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void updateStatusBarTime(String time) {
        binding.tvTime.setText(time);
    }

    @Override
    public void showPhoneError(String message) {
        if (message == null) {
            binding.tilPhone.setError(null);
        } else {
            binding.tilPhone.setError(message);
        }
    }

    @Override
    public void showPasswordError(String message) {
        if (message == null) {
            binding.tilPassword.setError(null);
        } else {
            binding.tilPassword.setError(message);
        }
    }

    @Override
    public void showLoading() {
        binding.btnLogin.setEnabled(false);
        binding.btnLogin.setText("登录中...");
    }

    @Override
    public void hideLoading() {
        binding.btnLogin.setEnabled(true);
        binding.btnLogin.setText(R.string.login);
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoginSuccess(int userType) {
        String userTypeStr = userType == LoginContract.USER_TYPE_BUYER ? "买家" : "卖家";
        Toast.makeText(this, userTypeStr + "登录成功", Toast.LENGTH_SHORT).show();
        
        // 保存用户类型到会话
        UserSession.getInstance().setUserType(userType);
        
        // 跳转到主页面
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("user_type", userType);
        startActivity(intent);
        finish(); // 关闭登录页面
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
        binding = null;
    }
} 